import sys

from PyQt4.QtGui  import QApplication, QAction, QKeySequence
from PyQt4.QtCore import Qt, SIGNAL
from PyQt4.Qsci   import QsciScintilla, QsciLexerPython

from core import cmds
from core.base import Plugin
from core.manager import PluginManager

class PythonEditor(QsciScintilla):
    def __init__(self, parent=None):
        QsciScintilla.__init__(self, parent)
        self.setUtf8(True)
        
        self.setLexer(QsciLexerPython(self))
        self.setEolMode(QsciScintilla.EolUnix)
        
        self.setAutoIndent(True)
        self.setIndentationsUseTabs(False)
        self.setIndentationWidth(4)
        self.setTabIndents(True)
        self.setBackspaceUnindents(True)
        self.setTabWidth(4)
        
        self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
        self.setMatchedBraceBackgroundColor(Qt.yellow)
        
        self.SendScintilla(QsciScintilla.SCI_INDICSETSTYLE,
                           QsciScintilla.INDIC_CONTAINER,
                           QsciScintilla.INDIC_BOX)
        self.SendScintilla(QsciScintilla.SCI_INDICSETFORE,
                           QsciScintilla.INDIC_CONTAINER,
                           0x4400FF)
        
        

class PythonScriptEditor(PythonEditor):
    def __init__(self, parent=None):
        PythonEditor.__init__(self, parent)
        self.exec_act = QAction(self)
        self.exec_act.setShortcut(QKeySequence.fromString("Ctrl+Return"))
        self.exec_act.setShortcutContext(Qt.WidgetShortcut)
        self.connect(self.exec_act, SIGNAL("triggered()"), self.execute)
        
        self.save_act = QAction(self)
        self.save_act.setShortcut(QKeySequence.fromString("Ctrl+s"))
        self.save_act.setShortcutContext(Qt.WidgetShortcut)
        self.connect(self.save_act, SIGNAL("triggered()"), self.save)
        
        self.addAction(self.save_act)
        self.addAction(self.exec_act)
                    
    def save(self):
        string = str(self.text())
        self.emit(SIGNAL("save"), string)
                
    def execute(self):
        self.convertEols(self.eolMode())
        string = str(self.selectedText()).strip()
        self.emit(SIGNAL("execute"), string)
        
class PluginEditorWidget(PythonEditor):
    def __init__(self, parent=None):
        PythonEditor.__init__(self, parent)
        self.plugin = None
        
        self.save_act = QAction(self)
        self.save_act.setShortcut(QKeySequence.fromString("Ctrl+s"))
        self.save_act.setShortcutContext(Qt.WidgetShortcut)
        self.connect(self.save_act, SIGNAL("triggered()"), self.save)
        
        self.addAction(self.save_act)
        
    def set_plugin(self, plugin):
        assert isinstance(plugin, Plugin)
        self.plugin = plugin
        self.refresh()
                
    def refresh(self):
        self.setText(self.plugin.code())
        
    def save(self):
        print 'SAVE'
        if self.plugin is None: return
        self.plugin.set_code(str(self.text()))
        PluginManager().load_plugin(self.plugin)
        PluginManager().commit_plugin(self.plugin)
        cmds.update_views()
                
if __name__ == '__main__':
    app = QApplication(sys.argv)
    editor = PythonScriptEditor(None)
    editor.show()
    app.exec_()